package cn.yanglq.Rss.web.controller.pdf;

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.Map;

/**
 * Created by ylq on 2017/8/2.
 */
@Controller
@RequestMapping("/pdf")
public class PdfController {

    @RequestMapping("pdfPage")
    public String PdfPage(HttpSession httpSession,
                          @RequestParam(value = "fileid", required = true) int fileid,
                          Map<String, Object> model) {
        model.put("fileid", fileid);
        return "pdf/pdf";
    }

    @RequestMapping("showPdf")
    public void showPdf(@RequestParam(value = "fileid", required = false) int fileid,
                        HttpServletResponse response) {
        try {
            File file = null;
            if (fileid == 1) {
                file = new File("D:/kindle推送教程.pdf");
            }
            FileInputStream fileInputStream = new FileInputStream(file);
            response.setHeader("Content-Disposition", "attachment;fileName=test.pdf");
            response.setContentType("multipart/form-data");
            OutputStream outputStream = response.getOutputStream();
            IOUtils.write(IOUtils.toByteArray(fileInputStream), outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    @RequestMapping("showPdf")
//    public String showPdf(@RequestParam(value = "fileid", required = false) int fileid,
//                          HttpServletResponse response) {
//        File file = null;
//        if (fileid == 1) {
//            file = new File("D:/文档处理.pdf");
//        }
//        String path = file.getPath();
//        return path;
//    }


//    /**
//     * 支持在线打开下载
//     *
//     * @throws IOException
//     */
//    @RequestMapping("pdfPage")
//    public void download(HttpServletResponse response, int fileid) throws IOException {
////        System.out.println("filePath:" + filePath);
//        String filePath = "D:/文档处理.pdf";
//        File f = new File(filePath);
//        if (!f.exists()) {
//            response.sendError(404, "File not found!");
//            return;
//        }
//        BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
//        byte[] bs = new byte[1024];
//        int len = 0;
//        response.reset(); // 非常重要
//        boolean isOnLine = true;
//        if (isOnLine) { // 在线打开方式
//            URL u = new URL("file:///" + filePath);
//            String contentType = u.openConnection().getContentType();
//            response.setContentType(contentType);
//            response.setHeader("Content-Disposition", "inline;filename="
//                    + "测试");
//            // 文件名应该编码成utf-8
//        } else {
//            // 纯下载方式
//            response.setContentType("application/x-msdownload");
//            response.setHeader("Content-Disposition", "attachment;filename="
//                    + "测试");
//        }
//        OutputStream out = response.getOutputStream();
//        while ((len = br.read(bs)) > 0) {
//            out.write(bs, 0, len);
//        }
//        out.flush();
//        out.close();
//        br.close();
//    }


}
